<script>
import {download, getmodel, query, remove} from "@/api/docMG";
import {EvevtBus} from "@/utils/EvevtBus";

export default {
  name: "PaperList",
  props: {
    doc_status: String,
  },
  data() {
    return {
      view_order: "time:asc",
      page: 1,
      total: 1,
      data: [],
      size: 10,
      multipleSelection: []
    }
  },
  mounted() {
    EvevtBus.$on('data', (data) => {
      this.data = data
    })
    if (this.$route.params.data !== undefined) {
      this.data = this.$route.params.data
    } else {
      this.query()
    }
  },
  methods: {
    edit(row) {
      this.$router.push({name: "certificateupload", params: {data: row}})

    },
    handleCurrentChange(val) {
      this.page = val
      this.query()
    },
    query() {
      query(this.page, this.size, this.doc_status, "certificate", this.view_order).then(res => {
        this.data = res.data.docs.records
        this.total = res.data.docs.total
      })
    },
    remove(row) {
      remove([row.id]).then(res => {
        if (res.code === 1) {
          this.$message.success(res.message)
          getmodel()
        } else this.$message.error(res.message)
      })
      setTimeout(() => this.query(), 500)
    },
    download(row) {
      download(row.id, row.modDbTable, row.title).then(res=>{
        const blob = new Blob([res]);//处理文档流
        const fileName = row.title+".pdf";
        const elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      EvevtBus.$emit('multipleSelection', val)
    }
  }
}
</script>

<template>
  <div id="file-list-content">
    <div class="achievement">
      <el-table :data="data" width="759px">
        <el-table-column
            type="selection"
            align="center"
            width="45">
        </el-table-column>
        <el-table-column
            align="center"
            prop="title"
            label="档案信息">
        </el-table-column>

        <el-table-column
            align="center"
            width="100"
            prop="puttime"
            :formatter="formatDate"

            label="获得时间"></el-table-column>
        <el-table-column
            align="center"
            width="155"
            label="操作">
          <template slot-scope="scope">
            <el-link :underline="false" @click.native="download(scope.row)">下载</el-link>
              <el-dropdown>
               <span>
                 更多
                 <i class="el-icon-arrow-down el-icon--right"></i>
               </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native=edit(scope.row)>编辑</el-dropdown-item>
                  <el-dropdown-item class="batch-delete" :divided=true  @click.native=remove(scope.row)>删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          background
          @current-change="handleCurrentChange"
          :page-size="15"
          layout="prev, pager, next"
          :total=total>
      </el-pagination>
    </div>
  </div>
</template>

<style scoped>
a {
  margin-right: 10px;
}

.batch-delete {
  color: #f56c6c;
}
</style>
